#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "h/binsearchtree.h"

typedef BinSeaTree Tree;
typedef Bstnode Node;
typedef BstCompCall Comp;

char menu(void);
void addpet(Tree *);
void droppet(Tree *);
void showpets(const Tree *);
void findpet(const Tree *);
void printitem(const Item * item);
void uppercase(char * st);

int petcomp(const Item *, const Item *);

char * s_gets(char *, int);
void eatline(void);

int main(void)
{
    Tree pets;
    char choice;

    InitializeBstree(&pets);
    while ((choice = menu()) != 'q')
    {
        switch (choice)
        {
            case 'a': addpet(&pets);
                break;

            case 'l': showpets(&pets);
                break;

            case 'f': findpet(&pets);
                break;

            case 'n': printf("%d pets in club\n",
                BstreeCountItem(&pets));
                break;

            case 'd': droppet(&pets);
                break;

            default: puts("Switching error");
        }
    }

    EmptyTheBstree(&pets);

    puts("Bye!");

    return 0;
}

char menu(void)
{
    int ch;

    puts("Nerfville  Pet Club Membership Program.");
    puts("Enter the letter corresponding to your choice:");
    puts("a) add a pet          l) show list of pets");
    puts("n) number of pets     f) find pets");
    puts("d) delete a pet       q) quit");

    while ((ch = getchar()) != EOF) {
        eatline();
        ch = tolower(ch);

        if (strchr("alnfdq", ch) == NULL)
            puts("Please enter an a, l, f, n, d, or q:");
        else
            break;
    }

    if (ch == EOF)
        ch = 'q';

    return ch;
}

void addpet(Tree * pt)
{
    Item temp;
    if (BstreeIsFull(pt))
        puts("No room in the club!");
    else {
        puts("Please enter name of pet:");
        s_gets(temp.petname, SLEN);
        puts("Please enter pet kind:");
        s_gets(temp.petkind, SLEN);

        uppercase(temp.petname);
        uppercase(temp.petkind);

        BstreeAddItem(&temp, pt, petcomp);
    }

}

void showpets(const Tree * pt)
{
    if (BstreeIsEmpty(pt))
        puts("No entries!");
    else
        Traverse(pt, printitem);
}

void printitem(const Item * it)
{
    printf("Pet: %-19s Kind: %-19s\n", it->petname, it->petkind);
}

void findpet(const Tree * pt)
{
    Item temp;
    if (BstreeIsEmpty(pt)) {
        puts("No entries!");
        return;
    }

    puts("Please enter name of pet you wish to find:");
    s_gets(temp.petname, SLEN);

    puts("Please enter pet kind:");
    s_gets(temp.petkind, SLEN);

    uppercase(temp.petname);
    uppercase(temp.petkind);

    printf("%s the %s ", temp.petname, temp.petkind);
    if (BstreeInTree(&temp, pt, petcomp))
        printf("is a member.\n");
    else
        printf("is not a member.\n");
}

void droppet(Tree * pt)
{
    Item temp;
    if (BstreeIsEmpty(pt))
    {
        puts("No entries!");
        return;
    }

    puts("Please enter name of pet you wish to delete:");
    s_gets(temp.petname, SLEN);

    puts("Please enter pet kind:");
    s_gets(temp.petkind, SLEN);

    uppercase(temp.petname);
    uppercase(temp.petkind);

    printf("%s the %s ", temp.petname, temp.petkind);
    if (BstreeDelItem(&temp, pt, petcomp))
        printf("is dropped from the club.\n");
    else
        printf("is not a member.\n");
}


int petcomp(const Item * pi1, const Item * pi2)
{
    int res;

    if ((res=strcmp(pi1->petname, pi2->petname)) != 0)
        return res;

    return strcmp(pi1->petkind, pi2->petkind);
}

void uppercase(char * str)
{
    while ((*str = toupper(*str)))
        str++;
}

char * s_gets(char * str, int n)
{
    char * ret_val;
    char * find;

    if ((ret_val = fgets(str, n, stdin))) {
        find = strchr(str, '\n');
        if (find)
            *find = 0;
        else
            while (getchar() != '\n')
                ;
    }

    return ret_val;
}

void eatline(void)
{
    while (getchar() != '\n')
        ;
}
